package com.enjoy.aop;

import com.enjoy.aop.advice.LogAfterAdvice;
import com.enjoy.aop.advice.LogBeforeAdvice;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

@Slf4j
public class AOPTest {

    @Test
    public void test1() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com.enjoy.aop");

        for (String name : context.getBeanDefinitionNames()) {
            log.info("bean name:{}", name);
        }

        //jdk的动态代理
        IB b = (IB) context.getBean("b");
        b.say();

        //cglib的代理
        A a = (A) context.getBean("a");
        a.say();

    }

    @Test
    public void test2() {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(new B());
        proxyFactory.setInterfaces(IB.class);
        proxyFactory.addAdvice(new LogBeforeAdvice());
        proxyFactory.addAdvice(new LogAfterAdvice());
        IB proxy = (IB) proxyFactory.getProxy();
        proxy.say();
    }

    @Test
    public void test3() {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(new B());
        proxyFactory.setInterfaces(IB.class);
        IB proxy = (IB) proxyFactory.getProxy();
        proxy.say();
    }
}
